<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BgcmsContentManager extends BgcmsProcess{
	var $db;
 	var $smarty;	
	public function __construct($db, $smarty) {
		$this->db = $db;
		$this->smarty = $smarty;
	}
		
	public function __destruct() {
		//
	}
	
	public function process() {
		$sub1 = 'category';
		if(isset($_GET['sub1'])) {
			$sub1 = $_GET['sub1'];
		}
		if($sub1 == "page") {
			$this->process_page($sub1);	
		}else
			$this->process_category($sub1);	
	}
	
	public function process_category($sub1) {
		if(isset($_POST['act'])){
			if($_POST['act'] == 'addnewcat') {
				BgcmsCategory::create_category();
			} elseif($_POST['act'] == 'delcat') {
				BgcmsCategory::delete_category();
			} elseif($_POST['act'] == 'editcat') {
				BgcmsCategory::edit_category();
			} elseif($_POST['act'] == 'setactive') {
				BgcmsCategory::setactive_category();
			} elseif($_POST['act'] == 'setdefault') {
				BgcmsCategory::setdefault_category();
			} elseif($_POST['act'] == 'assign_page') {
				BgcmsCategory::assign_page();
			} elseif($_POST['act'] == 'assign_gallery') {
				BgcmsCategory::assign_gallery();	
			} elseif($_POST['act'] == 'assign_product_list') {
				BgcmsCategory::assign_product_list();		
			} elseif($_POST['act'] == 'change_position') {
				BgcmsCategory::change_position();		
			} elseif($_POST['act'] == 'newpage') {
				BgcmsPage::create_page();
			} elseif($_POST['act'] == 'editpage') {
				BgcmsPage::edit_page();
			} elseif($_POST['act'] == 'setlimit') {
				BgcmsCategory::setLimit();
			}	
		} else {
			$act = "";
			if(isset($_GET["act"])) {
				$act = $_GET["act"];
			}
			if($act == "editcategory" || $act == "newcategory") {
				if($act == "editcategory"){	
					$catId = '';
					if(isset($_GET['id']))
						$catId = $_GET['id'];		
					$cat = BgcmsCategoryBean::get_category_by_id($this->db, $catId);
					if($cat->cat_onleft==1)
						$cat->oncheckleft = "checked";
					elseif($cat->cat_ontop==1)
						$cat->onchecktop = "checked";	
					$this->smarty->assign("THECATONE", $cat);
				}
				$parent_name = BgcmsCategoryBean::get_category_by_id($this->db,$_GET['catid']);
				$this->smarty->assign("PARENT_NAME",$parent_name->cat_name);
			} else if($act == "assign_page"){
				$exixts = BgcmsPageBean::get_page2cat_by_catid($this->db,$_GET['id']);
				if($exixts!=""){
					//Load page information
					$id = $exixts->page_id;
					if($id != "") {
						$page = BgcmsPageBean::get_page_by_id($this->db, $id, $_SESSION['LC_ALL']);
						$this->smarty->assign("THEPAGE", $page);
						$selcats = BgcmsCategoryBean::get_category_by_page($this->db, $id);
						$arrCat = array();
						if($selcats != null) {
							foreach($selcats as $cat) {
								$arrCat[count($arrCat)] = $cat->category_id;
							}
						}
						$this->smarty->assign('cat_selected_id', $arrCat);
					}
				}	
				$allpage =	BgcmsPageBean::get_all_page($this->db);
				if($allpage!=""){
					foreach($allpage as $item){
						if($item->page_id == $item->active_page){
							if($item->category_id == $_GET['id'])
								$item->active = "checked";
							else
								$item->active ="disabled";	
						}	
					}
				}
				
				$catname = BgcmsCategoryBean::get_catandparent_by_id($this->db,$_GET['id']);
				if($catname->parentname!="")
					$catname->cat_name = $catname->parentname."&nbsp;&raquo;&nbsp;".$catname->cat_name;
				$this->smarty->assign("CATNAME",$catname);
				$this->smarty->assign("EXISTED", $exixts);
				$this->smarty->assign("ALLPAGES", $allpage);
			
			} else if($act == "position"){
				if(isset($_GET['subcat']) && $_GET['subcat']!="")
					$id = $_GET['catid'];
				else
					$id = 0;
				$cat = BgcmsCategoryBean::get_category_by_id($this->db, $_GET['id']);		
				$listcat = BgcmsCategoryBean::get_category_by_level($this->db,$id,$_GET['id']);
				$this->smarty->assign("THECAT", $cat);
				$this->smarty->assign("CATLEVEL", $listcat);
			} else {
				$categories = array();
				$id = "";
				if(isset($_GET['subcat']) && (isset($_GET['catid']) && $_GET['catid'] !=""))
				{
					$id = $_GET['catid'];
					//get cat_parent for sub cat paging
					//$link = "?module=BgcmsContentManager&sub1=category&act=".$_GET['subcat']."&id=".$_GET['id'];
					$link = "?module=BgcmsContentManager&sub1=&subcat=".$_GET['subcat']."&catid=".$_GET['catid'];
					$parent = BgcmsCategoryBean::get_category_by_id($this->db,$id);
					$categories['parent_name'] = $parent->cat_name; 
				}
				else
				{
					$link = "?module=BgcmsContentManager&sub1=category";
				}
				$countcat = BgcmsCategoryBean::count_category($this->db,$id);
				
				$categories['data'] = BgcmsCategoryBean::get_category_paging($this->db,ADMINPAGING,"list",$id,$_GET['sort'],$_GET['by']);
				
				$result = BgcmsPaging::do_paging($countcat,$categories, ADMINPAGING, $link,"list");
				if($result['data']!=""){
					foreach($result['data'] as $item)
					{
						$exixts = BgcmsPageBean::get_page2cat_by_catid($this->db,$item->cat_id);
						if($exixts!="")
							$item->exixts = '1';
					}
				}	
				
				$default = BgcmsCategoryBean::get_default_category($this->db);
				$result['default_id']= $default->cat_id;
				$result['default_name']= $default->cat_name;
				$this->smarty->assign("LISTCATS", $result);
			}
			
			$catList = BgcmsCategoryBean::get_all_category($this->db, $catId);
			$this->smarty->assign("CATS", $catList);
			$this->Run();
		}
	}
	
	public function process_page($sub1) {
	
		$catList = BgcmsCategoryBean::get_all_category($this->db, null);
		if(isset($_POST['act'])) {
			if($_POST['act'] == 'newpage') {
				BgcmsPage::create_page('');
			} elseif($_POST['act'] == 'delpage') {
				BgcmsPage::delete_page();
			} elseif($_POST['act'] == 'editpage') {
				BgcmsPage::edit_page('');
			}
		} else {
			$act = "";
			if(isset($_GET["act"])) {
				$act = $_GET["act"];
			}
			if($act == "newpage" || $act == "editpage") {
				$users = BgcmsUserBean::get_all_user($this->db);	
				$this->smarty->assign("USERS", $users);
				
				if($act == "editpage") {
					$id = "";
					if(isset($_GET['id'])) {
						//Load page information
						$id = $_GET['id'];
						if($id != "") {
							foreach($catList as $item)
							{
								$check = BgcmsPageBean::get_page2cat_by_id($this->db,$item->cat_id, $id);
								if($check!="")
									$item->checked = "checked";			
							}
							$page = BgcmsPageBean::get_page_by_id($this->db, $id, $_SESSION['LC_ALL']);
							$this->smarty->assign("THEPAGE", $page);
							$selcats = BgcmsCategoryBean::get_category_by_page($this->db, $id);
							$arrCat = array();
							if($selcats != null) {
								foreach($selcats as $cat) {
									$arrCat[count($arrCat)] = $cat->category_id;
								}
							}
							$this->smarty->assign('cat_selected_id', $arrCat);
						}
					}
				}
			}else{
				$pages = array();
				if(isset($_POST["s"])) {
					$keyword = $_POST["s"];
				}
				if(isset($_POST["cat"])) 
				{
					$cat = $_POST["cat"];
				}
				$countpage =  BgcmsPageBean::count_all_page($this->db, $keyword,$cat ,$_SESSION["LC_ALL"]);
				$pages['data'] = BgcmsPageBean::get_all_page_paging($this->db, $keyword,$cat ,$_SESSION["LC_ALL"],ADMINPAGING,"list");
				$result = BgcmsPaging::do_paging($countpage,$pages, ADMINPAGING, "?module=BgcmsContentManager&sub1=page","list");
				$this->smarty->assign('PAGES', $result); 
			}	
			
			if(isset($_SESSION['page_message'])) {
				$this->smarty->assign('page_message', $_SESSION['page_message']);
				unset($_SESSION['page_message']);
			}
			
			$this->smarty->assign_by_ref('CATS', $catList);
			$this->Run();
		}
	}
}
?>